home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / lib / mathlib / libfft / TRY / r_check3d.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.1 KB  |  232 lines

  1. #include <stdio.h>
  2. #include <sys/time.h>
  3.  
  4. #include "fft.h"
  5. #include "constant.h"
  6.  
  7. /*                        */
  8. /*    Precision dependant declarations    */
  9. /*                        */
  10. #ifdef    DOUBLE
  11.  
  12. #define TOLERANCE   DTOLERANCE
  13. typedef        double    this_half;
  14. typedef        double    this_type;
  15.  
  16. #define        THIS_SUM    dsum_
  17. #define        THIS_GEN    dgen_
  18. #define        THIS_FT    dft3d_
  19.  
  20. #define        THIS_FFTI    dfft3di
  21. #define        THIS_FFT    dfft3d
  22. #define        THIS_SCAL    dscal3d
  23.  
  24. #endif
  25. #ifdef    SINGLE
  26.  
  27. typedef        float        this_half;
  28. typedef        float        this_type;
  29.  
  30. #define TOLERANCE   STOLERANCE
  31.  
  32. #define        THIS_SUM    ssum_
  33. #define        THIS_GEN    sgen_
  34. #define        THIS_FT    sft3d_
  35.  
  36. #define        THIS_FFTI    sfft3di
  37. #define        THIS_FFT    sfft3d
  38. #define        THIS_SCAL    sscal3d
  39.  
  40. #endif
  41.  
  42. /*                        */
  43.  
  44. this_half THIS_SUM();
  45.  
  46. void inimat_();
  47. void GetArguments();
  48. void get_values();
  49.  
  50. int size_x, ld1, ld2, size_z , size_y, n_trials;
  51. this_type *pa, *pb, *pRef, *pSave;
  52.  
  53. main(argc,argv)
  54. int argc;
  55. char *argv[];
  56. {
  57.     int i, j, k, n_total, is_wrong, nerr, inc;
  58.     double x, y, dx, dy, emax, sum, err, maxerr;
  59.     this_half    t;
  60.     this_type *ptr, *pt;
  61.  
  62. /* ******************************************************* */
  63.     GetArguments( argc, argv);
  64. /* ******************************************************* */
  65.  
  66.     srandom( (123*getpid()) | 0x01);
  67.  
  68.     for( ; n_trials > 0; n_trials --) { 
  69.     get_values();
  70.  
  71.     printf("\n");
  72.     printf( "%3d <%3d,%3d,%3d>", n_trials, size_x, size_y, size_z);
  73.     fflush(stdout);
  74.  
  75.     ld1 = ((size_x + 2)/2*2);
  76.     ld2 = size_y + 1;
  77.     n_total = (ld1*ld2*(size_z+1));
  78.     pa = (this_type *)malloc
  79.         ( (3 * n_total + size_x + 3* size_y + 3*size_z + 10 * FACTOR_SPACE) * sizeof(this_type));
  80.     if( !(pa) ) {
  81.         fprintf( stderr, "Could not allocate ... Exiting\n");
  82.         exit (-1);
  83.     }
  84.     pb = pa + n_total;
  85.     pRef = pb + n_total;
  86.     pSave = pRef + n_total;
  87.  
  88. /* *******************************************************
  89.     Initialize arrays
  90. ******************************************************* */
  91.     THIS_GEN(pRef, &n_total);
  92.     bcopy( pRef, pa, n_total*sizeof(this_type));
  93.     bcopy( pRef, pb, n_total*sizeof(this_type));
  94.  
  95. /* *******************************************************
  96.     PACKED direct Fourier Transform
  97. ******************************************************* */
  98.         j = -1;
  99.     THIS_FT( &j, &size_x, &size_y, &size_z, pb, &ld1, &ld2);
  100.     pSave = THIS_FFTI( size_x, size_y, size_z, pSave);
  101.     is_wrong = THIS_FFT( -1, size_x, size_y, size_z, pa, ld1, ld2, pSave);
  102.  
  103.     emax = TOLERANCE*(size_x * size_y);
  104.     for(i = 0, nerr=0 ; i < n_total ; i ++) {
  105.         x = pa[i] - pb[i];
  106.         if( (t= x*x) > (emax)) {
  107.         nerr++;
  108.         }
  109.     }
  110.     if( !(nerr)){
  111.         fprintf( stderr, ".");
  112.     }
  113.     else { 
  114.         fprintf( stderr, "@%d@", nerr);
  115.     }
  116.     inc = 1;
  117.     x = 0;
  118.     for( j= 0, ptr = pRef ; j < size_z ; j++, ptr += ld1*ld2) { 
  119.         for ( i = 0, pt = ptr; i < size_y; i++, pt += ld1) { 
  120.         x +=  THIS_SUM( &size_x, pt, &inc);
  121.         }
  122.     }
  123.     dx = x - pa[0];
  124.     if( (t= dx *dx) > TOLERANCE){
  125.         fprintf( stderr, 
  126.         "\nError direct FFT(%d,%d) at index #0 : (%f)<->(%f)error(%f)",
  127.         size_x, size_y, pa[0], x, dx);
  128.     }
  129.     dx = x - pb[0];
  130.     if( (t= dx *dx) > TOLERANCE){
  131.         fprintf( stderr, 
  132.         "\nError direct DFT(%d,%d) at index #0 : (%f)<->(%f)error(%f)",
  133.         size_x, size_y, pb[0],x,dx);
  134.     }
  135. /* *******************************************************
  136.     PACKED inverse Fourier Transform
  137. ******************************************************* */
  138.     is_wrong = THIS_FFT( 1, size_x, size_y, size_z, pa, ld1, ld2, pSave);
  139.  
  140.     t = 1. / (double)(size_x * size_y * size_z);
  141.  
  142.     THIS_SCAL( size_x, size_y, size_z, t, pa, ld1, ld2);
  143.  
  144.     emax = TOLERANCE;
  145.     emax = emax * emax;
  146.  
  147.     sum = 0.;
  148.     err= 0.;
  149.     nerr= 0;
  150.     maxerr= 0.;
  151.     for(i = 0 ; i < n_total ; i ++) {
  152.         sum += (pRef[i] * pRef[i]) + (pRef[i] * pRef[i]);
  153.         x = pa[i] - pRef[i];
  154.         if( (t= x*x) > (emax))
  155.         nerr++;
  156.         err += t;
  157.         if( t > maxerr) maxerr = t;
  158.     }
  159.     err = sqrt(err / (double)(size_x*size_y));
  160.     sum = sqrt(sum / (double)(size_x*size_y));
  161.     maxerr = sqrt(maxerr);
  162.     printf(": avg:(%8.3g /%8.3g)= %8.3g <> maxerr: (%8.3g /%8.3g)= %8.3g", 
  163.     err, sum, err/sum, maxerr, sum, maxerr/sum);
  164.     if(nerr){
  165.         printf("\n%d errors detected \n", nerr);
  166.         exit(-2);
  167.     }
  168.     free(pa);
  169.     }
  170.     printf("\n");
  171.     return(0);
  172. }
  173.  
  174.  
  175. int is_random;
  176.  
  177. void GetArguments( argc, argv)
  178. int argc;
  179. char *argv[];
  180. {
  181.     int i, j, k;
  182.     int nerror = 0;
  183.  
  184. #define ON    1
  185.  
  186.     n_trials = DEF_TRIALS;
  187.     is_random = 1;
  188.  
  189. /* ******************************************************* */
  190.     for ( i = 1 ; (i < argc) && (nerror != ON) ; i ++ ) {
  191.     if( argv[i][0] == '-') {
  192.         switch ( argv[i][1]) {
  193.         case 'i' :
  194.         case 'I' :  
  195.                 is_random = 0;
  196.                 break;
  197.         default  :  nerror = ON;
  198.         }
  199.     }
  200.     else {
  201.         if( i+1 > argc)
  202.         nerror = ON;
  203.         else { 
  204.         n_trials = atoi( argv[i]);
  205.         }
  206.     }
  207.     }
  208.     if( nerror == ON) {
  209.     fprintf( stderr, 
  210.         "Usage : %s [-p(arallel)] [n_trials]\n", argv[0]);
  211.     exit(-1);
  212.     }
  213. }
  214.  
  215. void get_values()
  216. {
  217.   if( is_random){ 
  218.     size_x = random() % MAX_3D + 1;
  219.     size_y = random() % MAX_3D + 1;
  220.     size_z = random() % MAX_3D + 1;
  221.     if( !(n_trials % 5))
  222.     size_z = size_y = size_x;
  223.   } else{
  224.     printf( "Enter SIZE_X : ");
  225.     scanf("%d", &size_x);
  226.     printf( "Enter SIZE_Y : ");
  227.     scanf("%d", &size_y);
  228.     printf( "Enter SIZE_Z : ");
  229.     scanf("%d", &size_z);
  230.   }
  231. }
  232.